package com.bjhb.wuranyuan.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjhb.wuranyuan.NumberUtil;
import com.bjhb.wuranyuan.dao.FactInfoMapper;
import com.bjhb.wuranyuan.dao.QyxxMapper;
import com.bjhb.wuranyuan.dao.XzqhMapper;
import com.bjhb.wuranyuan.pojo.FactInfo;
import com.bjhb.wuranyuan.pojo.Qyxx;
import com.bjhb.wuranyuan.vo.FactInfoVO;

@Controller
public class FactInfoAction {

	@Resource
	private FactInfoMapper factInfoMapper;

	@Resource
	private XzqhMapper xzqhMapper;
	@Resource
	private QyxxMapper qyxxMapper;

	@RequestMapping("/fact/edit.htm")
	public String edit(HttpServletRequest request, String factId, String qydm) {
		Qyxx qyxx = qyxxMapper.selectByQydm(qydm);
		request.setAttribute("qyxx", qyxx);
		FactInfo fact = factInfoMapper.query(factId);
		setCodes(request);
		request.setAttribute("fact", fact);
		return "fact/fact_edit";

	}

	@RequestMapping("/fact/save.htm")
	public String save(HttpServletRequest request, FactInfoVO fact) {
		System.out.println(fact);
		factInfoMapper.update(fact.getPojo());
		Qyxx qyxx = fact.getQyxx();
		qyxxMapper.update(qyxx);
		setCodes(request);
		request.setAttribute("flg", "true");
		return "fact/fact_edit";
	}

	@RequestMapping("/fact/add.htm")
	public String add(HttpServletRequest request, FactInfoVO fact) {
		System.out.println(fact);
		Qyxx qyxx = fact.getQyxx();
		factInfoMapper.insert(fact.getPojo());
		String qydm = NumberUtil.getNext(qyxxMapper.getMaxQydm());
		qyxx.setQydm(qydm);
		qyxxMapper.insert(qyxx);
		setCodes(request);
		request.setAttribute("flg", "true");
		return "fact/fact_edit";
	}

	private void setCodes(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("table", "code_fact_kind");
		request.setAttribute("kinds", factInfoMapper.getCodes(map));
		map.put("table", "CODE_FACTSCALE");
		request.setAttribute("scales", factInfoMapper.getCodes(map));
		map.put("table", "code_havingtype");
		request.setAttribute("havings", factInfoMapper.getCodes(map));
		map.put("table", "code_trade");
		request.setAttribute("trades", factInfoMapper.getCodes(map));
		request.setAttribute("focus", factInfoMapper.getFocusCodes());
		request.setAttribute("xzqhs", xzqhMapper.query());
	}
}
